<?php


namespace App\Http\Controllers\app\product;

use App\Http\Controllers\common\CommonFun;
use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Http\Controllers\BranchUtils;
use App\Http\Controllers\app\product\utils as ProductUtils;
use App\Http\Controllers\Utils;
use App\Http\Resources\Product\Product as ProductProduct;
use App\Models\Order;
use App\Models\Product;
use App\Models\ProductClass;
use App\Models\User;
use App\Models\Worker;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    // 产品类型查询
    public function SeleProductClassify(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm('user'), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        $user_id = $tokenInfo['user_id'];
        $all = $request->all();
        $data_dic = BranchUtils::get_user_branch($user_id);
        if ($data_dic['code'] == 200) {
            $branch_id = $data_dic['msg'];
        } else {
            return LMReturn::Error($data_dic['msg']);
        }
        $where[] = ['status', 1];
        $where[] = ['app_status', 1];
        $product_classify_list = ProductClass::where($where)->whereHas('branches', function ($query) use ($branch_id) {
            $query->where('branch_id', $branch_id);
        })->get();
        return LMReturn::Success(CommonFun::ArrToTreeList(json_decode($product_classify_list, true), "id", "pid"));
    }

    // 根据产品类型查询产品列表
    public function SeleProducts(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm('user'), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        $user_id = $tokenInfo['user_id'];
        $all = $request->all();
        $data_dic = BranchUtils::get_user_branch($user_id);
        if ($data_dic['code'] == 200) {
            $branch_id = $data_dic['msg'];
        } else {
            return LMReturn::Error($data_dic['msg']);
        }
        $where = [];
        if (isset($all['classify_id'])) {
            $classify_list = ProductUtils::get_classify($branch_id, $all['classify_id']);
        } else {
            $classify_list = ProductUtils::get_classify($branch_id);
        }
        if (isset($all['tag'])) {
            $where[] = ['tag', 'like', '%' . $all['tag'] . '%'];
        }
        $size = $all['size'] ?? 15;
        $product_list = Product::whereIn('class_id', $classify_list)
            ->where($where)
            ->where('state', 1)
            ->where('app_status', 1)
            ->with('product_class')
            ->paginate($size);
        // return LMReturn::Success($product_list);
        $res_product_list = ProductProduct::collection($product_list);
        $return_datas = Utils::deal_page_resources($res_product_list, $product_list);
        return LMReturn::Success($return_datas);
        /*
        if (empty($all['classify_id'])) {
            if ($classify_list) {
                $product_list = Product::whereIn('class_id', $classify_list)
                    ->where('state', 1)
                    ->where('app_status', 1)
                    ->with('product_class')
                    ->get();
            } else {
                return LMReturn::Error('抱歉您没有权限查看');
            }
        } else {
            if (in_array($all['classify_id'], $classify_list)) {
                $class_id = $all['classify_id'];
                $class_list[] = $class_id;
                $classifies = ProductClass::where('pid', $class_id)->get();
                if ($classifies) {
                    foreach ($classifies as $classify) {
                        if (in_array($classify['id'], $classify_list)) {
                            $class_list[] = $classify['id'];
                        }
                    }
                }
                $product_list = Product::whereIn('class_id', $class_list)->where('state', 1)->where('app_status', 1)->with('product_class')->get();
            } else {
                return LMReturn::Error('抱歉您没有权限查看');
            }
        }
        if (isset($all['tag'])) {
            $products = utils::deal_product_result($product_list, $all['tag']);
        } else {
            $products = utils::deal_product_result($product_list);
        }
        $size = $all['size'] ?? 15;
        $page = $all['page'] ?? 1;
        return LMReturn::Success(utils::Paging($products, $page, $size));
        */
    }

    // 查询某个用户买过的产品
    public function SeleUserProduct(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm('user'), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        $user_id = $tokenInfo['user_id'];
        $all = $request->all();
        $user = User::find($user_id);
        if ($user) {
            $products = [];
            $orders = Order::where('user_id', $user_id)->whereIn('order_status', [1, 2, 4, 6])->get();
            if ($orders) {
                foreach ($orders as $order) {
                    if (!in_array($order->product_id, $products)) {
                        $products[] = $order->product_id;
                    }
                }
            }
            if ($products) {
                $product_list = Product::whereIn('id', $products)->where('state', 1)->where('app_status', 1)->with('product_class')->get();
                $products = ProductUtils::deal_product_result($product_list);
                $size = $all['size'] ?? 15;
                $page = $all['page'] ?? 1;
                return LMReturn::Success(ProductUtils::Paging($products, $page, $size));
            } else {
                return LMReturn::Success();
            }
        } else {
            return LMReturn::Error('无该用户');
        }
    }
}
